module ApplicationHelper
	
	include Permissible
	
	# Returns the full title on a per-page basis.
	def title(page_title="")
		base_title = "Uni Ulm DMS"
		if page_title.empty?
			base_title
		else
			"#{base_title} | #{page_title}"
		end
	end
	
	def current_menu_name(controller=params[:controller], action=params[:action])
		case controller
		when 'categories'
			'archive'
		when 'documents'
			if action == 'new'
				'home'
			else				
				'archive'
			end
		else
			'home'
		end
	end
	
	def current_menu_entries
		case current_menu_name
		when 'archive'
			menu = Category.find_all_by_parent_id(nil).sort_by { |c| c.order_position }
			menu = menu.map { |cat| [cat.name, cat] }
		else
			menu = [
				[t('navigation.news'), home_path],
				[t('navigation.create_document'), '/documents/new'],
				[t('navigation.groups'), '#']
			]
			if logged_in?
				menu += [
					[t('navigation.my_documents'), '#'],
					[t('navigation.personal_data'), edit_user_path(current_user)],
					[t('navigation.preferences'), '#']
				]
			end
			return menu
		end
	end
	
	def id_to_user_link(id)
		unless id =~ /[0-9]/
			return id
		end
		user = User.find(id)
		link_to user.name, user
	end
	
	# Helper method to check permissions in views
	def has_permission_for?(namespace, action, resource_id=nil)
		permissible?(action, resource_id, namespace)
	end
	
end
